<template>
  <div class="hm-login">
    <div class="title">面经注册</div>
    <div class="name">
      <span>用户名</span
      ><input type="text" placeholder="用户名" v-model="username" />
    </div>

    <div class="passward">
      <span>密码</span
      ><input type="text" placeholder="密码" v-model="password" />
    </div>
    <div class="button" @click="register">注册</div>
    <!-- <router-link to="/login"><a href="#">有账号，去登录</a></router-link> -->
    <router-link to="/login">有账号，去登录</router-link>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    register() {
      if (this.username.length < 5) {
        alert("用户名长度不能小于5位");
        return;
      }
      if (this.password.length < 6) {
        alert("密码长度不能小于6位");
        return;
      }
    //   console.log(this.username.length);
      axios({
        url: "http://interview-api-t.itheima.net/h5/user/register",
        method: "post",
        data: { username: this.username, password: this.password },
      }).then((res) => {
        if (res.status === 200) {
          alert("注册成功");
          this.$router.push("/login");
        } 
      })
      .catch(() => {
        alert("注册失败");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.hm-login {
  width: 100%;
  position: relative;
  .title {
    text-align: center;
    margin: 10px auto;
    line-height: 46px;
    font-weight: 600;
    font-size: 20px;
  }
  .name {
    height: 44px;
    border-bottom: 1px solid #ebedf0;
    display: flex;
    align-items: center;
    padding: 10px 16px;
    span {
      margin-right: 10px;
      font-size: 20px;
      width: 90px;
      height: 24px;
      // background-color: rebeccapurple;
    }
    input {
      flex: 1;
      width: 244px;
      height: 24px;
      border: none;
      outline: none;
    }
  }
  .passward {
    height: 44px;
    border-bottom: 1px solid #ebedf0;
    display: flex;
    align-items: center;
    padding: 10px 16px;
    span {
      margin-right: 10px;
      font-size: 20px;
      width: 90px;
      height: 24px;
      // background-color: rebeccapurple;
    }
    input {
      flex: 1;
      width: 244px;
      height: 24px;
      border: none;
      outline: none;
    }
  }
  .button {
    width: 343px;
    height: 44px;
    background-color: #07c160;
    color: #fff;
    line-height: 44px;
    text-align: center;
    border-radius: 10px;
    margin-top: 20px;
  }
  a {
    height: 16px;
    padding-right: 20px;
    position: absolute;
    right: 0;
    top: 270px;
    font-size: 3.2vw;
  }
}
</style>
